package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.mapper.AppUserDataMapper;
import com.zhentao.model.common.app.dto.UserLoginDto;
import com.zhentao.model.common.app.pojo.AppUser;
import com.zhentao.model.common.app.pojo.AppUserData;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.AppUserService;
import com.zhentao.mapper.AppUserMapper;
import com.zhentao.util.UserThreadLocalutil;
import com.zhentao.utils.common.AppJwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
* @author 贾晓婷
* @description 针对表【app_user】的数据库操作Service实现
* @createDate 2024-09-26 11:55:20
*/
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser>
    implements AppUserService{

    @Resource
    AppUserMapper appUserMapper;
    @Resource
    AppUserDataMapper appUserDataMapper;

    @Override
    public ResponseResult login(UserLoginDto userLoginDto) {
        String security = userLoginDto.getSecurity();
        //查询数据库
        QueryWrapper<AppUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(AppUser::getPhone,userLoginDto.getPhone());
        //查询
        AppUser appUser = this.appUserMapper.selectOne(queryWrapper);
        System.out.println("手机号查询到的详细信息 = " + appUser);
        //判断
        if(appUser==null){
            //注册
            ResponseResult responseResult = addUser(userLoginDto);
            Object data = responseResult.getData();
            AppUser appUser1= (AppUser) data;

            //生成token
            String token = AppJwtUtil.getToken(Long.valueOf(appUser1.getUserId()));
            //创建map集合
            HashMap<String,Object> map =new HashMap<>();//将token放入集合当中
            map.put("token",token);
            //将用户信息放入map当中
            map.put("appLoginUser",appUser1);
            return ResponseResult.okResult(map);

        }else if(!appUser.getSecurity().equals(security)){
            return ResponseResult.errorResult(401,"密码错误");
        }else{
            //生成token
            String token = AppJwtUtil.getToken(Long.valueOf(appUser.getUserId()));
            //创建map集合
            HashMap<String,Object> map =new HashMap<>();//将token放入集合当中
            map.put("token",token);
            //将用户信息放入map当中
            map.put("appLoginUser",appUser);
            return ResponseResult.okResult(map);
        }
    }

    @Override
    public ResponseResult selectByUid() {
        AppUser user = UserThreadLocalutil.getUser();
        Integer userId = user.getUserId();
        AppUser appUser = appUserMapper.selectById(userId);
        Integer userDataId = appUser.getUserDataId();
        AppUserData appUserData = appUserDataMapper.selectById(userDataId);
        appUser.setAppUserData(appUserData);

        return ResponseResult.okResult(appUserData);
    }

    /**
     * 用户注册
     * @param userLoginDto
     * @return
     */
    private ResponseResult addUser(UserLoginDto userLoginDto) {
        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(userLoginDto,appUser);
        //生成字母乱码设置为appUser内的昵称
        StringBuilder nickname = new StringBuilder();

        for (int i = 0; i < 6; i++) {
            char randomChar = (char) (Math.random() * 26 + 'a'); // 生成随机小写字母
            nickname.append(randomChar); // 添加到昵称中
        }
        this.appUserMapper.insert(appUser);


        //添加到资料表
        AppUserData appUserData = new AppUserData();
        appUserData.setName(nickname.toString());
        appUserData.setAvatar("http://b.mtw.so/6asrih");

        //生成资料ID   6位数字的
        int randomNum = (int)(Math.random() * 1000000); // 生成0到999999之间的整数
        appUserData.setMemberId(Integer.valueOf(String.format("%06d", randomNum))); // 格式化为6位数，不足的用0填充

        appUserDataMapper.insert(appUserData);
        Integer userDataId = appUserData.getUserDataId();
        appUser.setUserDataId(userDataId);
        appUserMapper.updateById(appUser);
        return ResponseResult.okResult(appUser);
    }

}




